<template>
  <div class="goods-list">
    <div
      class="goods-item"
      v-for="(item,index) in goodslist"
      :key="index"
      @click="getDetail(item.id)"
    >
      <img :src="item.img_url" alt />
      <h1 class="title">{{item.title}}</h1>
      <div class="info">
        <p class="price">
          <span class="now">￥{{item.sell_price}}</span>
          <span class="old">￥{{item.market_price}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{item.stock_quantity}}件</span>
        </p>
      </div>
    </div>
    <mt-button type="danger" @click="getMore" size="large">加载更多</mt-button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      pageIndex: 1,
      goodslist: []
    };
  },
  created() {
    this.getgoodsList();
  },
  methods: {
    getgoodsList() {
      this.$http.get("api/getgoods?pageindex" + this.pageIndex).then(result => {
        if (result.data.status === 0) {
          // console.log(result);
          this.goodslist = this.goodslist.concat(result.data.message);
        }
      });
    },
    getMore() {
      this.pageIndex++;
      this.getgoodsList();
    },
    getDetail(id) {
      // this.$router.push("/home/goodsinfo/" + id);
      // 2. 传递对象
      // this.$router.push({ path: "/home/goodsinfo/" + id });
      // 3. 传递命名的路由(需要在定义的路由规则对象中添加name属性)
      // this.$router.push("/home/goodsinfo" + index);
      // 2传递对象
      // this.$router.push({ path: "/home/goodsinfo" + index });
      // 命名路由传递
      // this.$route 是路由【参数对象】，params和query搜属于它
      //this.$router是一个【路由导航对象】，用它可以方便的使用js代码实现跳转，前进和后退
      this.$router.push({ name: "goodsinfo", params: { id } });
    }
  }
};
</script>
<style lang="scss" scoped>
.goods-list {
  display: flex;
  //灵活属性，必要时拆行拆列 强制换行
  flex-wrap: wrap;
  padding: 7px;
  justify-content: space-between;

  .goods-item {
    width: 49%;
    border: 1px solid #ccc;
    box-shadow: 0 0 8px #ccc;
    margin: 4px 0;
    padding: 2px;
    display: flex;
    //垂直显示 flex布局中的 切换主轴的方向 默认为x
    flex-direction: column;
    // 两边对齐 中间：center 左边： 右边:
    justify-content: space-between;
    // 最小高度
    min-height: 293px;
    img {
      width: 100%;
    }
    .title {
      font-size: 14px;
    }
    .info {
      background-color: #eee;
      p {
        margin: 0;
        padding: 5px;
      }
      .price {
        .now {
          color: red;
          font-weight: bold;
          font-size: 16px;
        }
        .old {
          // 相当于是设置的删除线
          text-decoration: line-through;
          font-size: 12px;
          margin-left: 10px;
        }
      }
      .sell {
        display: flex;
        justify-content: space-between;
        font-size: 13px;
      }
    }
  }
}
</style>
